using System;
using UnityEngine;

[Serializable]
public class TTCameraBehaviourPlayCamera : TTCameraBehaviour
{
	public Vector3 offsetFromHitPoint;

	public FloatRange distanceRange = default(FloatRange);

	public float minDistance = 1f;

	public FloatRange heightRange = default(FloatRange);

	public Vector3 lookAtPoint;

	public float lookStraightStrength = 1f;

	public float minMoveDistance = 0.2f;

	public bool usePlayerPosition;

	protected TableTennisHumanPlayer humanPlayer;

	public bool updateWhenBallNotGoingTowardsPlayer;

	public bool useOrigPositionForOffset;

	public Vector3 debugPosition;

	public float debugOffset;

	public override void Start(Camera cam, TTCameraController camController, TTCameraOptions cameraOptions)
	{
		base.Start(cam, camController, cameraOptions);
		humanPlayer = cameraOptions.player as TableTennisHumanPlayer;
	}

	public override void Update()
	{
		if (humanPlayer == null)
		{
			return;
		}
		if (!humanPlayer.ballFlyingTowardsMe)
		{
			if (updateWhenBallNotGoingTowardsPlayer)
			{
				if (usePlayerPosition)
				{
					UpdateCameras(humanPlayer.transform.position.OnGround());
					return;
				}
				Vector3 vector = (humanPlayer.hitPosition.x - humanPlayer.playerPositionWhenHit.x) * Vector3.right + humanPlayer.transform.position;
				UpdateCameras(vector.OnGround());
			}
		}
		else if (usePlayerPosition)
		{
			UpdateCameras(humanPlayer.playerPositionWhenHit.OnGround());
		}
		else
		{
			UpdateCameras(humanPlayer.hitPosition.OnGround());
		}
	}

	private void UpdateCameras(Vector3 position)
	{
		debugPosition = position;
		Vector3 vector = offsetFromHitPoint;
		vector.z = Mathf.Abs(vector.z) * (float)humanPlayer.tableSide;
		Vector3 vector2 = position + vector;
		Vector3 vector3 = lookAtPoint;
		vector3.x = Mathf.Lerp(lookAtPoint.x, vector2.x, lookStraightStrength);
		vector2.z = Mathf.Sign(vector2.z) * Mathf.Max(Mathf.Abs(vector2.z), minDistance * Table.halfLength);
		vector2.x = position.x + Mathf.Sign(humanPlayer.transform.position.x - position.x) * vector.x;
		Vector3 vector4 = vector2;
		if (useOrigPositionForOffset)
		{
			vector4 = position;
		}
		float value = distanceRange.InverseLerp(Mathf.Abs(vector4.z) / Table.halfLength);
		debugOffset = Mathf.Abs(vector4.z) / Table.halfLength;
		float num = heightRange.Lerp(value);
		vector2.y += num;
		Quaternion b = Quaternion.LookRotation(vector3 - vector2);
		int num2 = 1;
		camera.transform.position = Vector3.Lerp(camera.transform.position, vector2, RealTime.deltaTime * moveSpeed * (float)num2);
		camera.transform.rotation = Quaternion.Lerp(camera.transform.rotation, b, RealTime.deltaTime * rotationSpeed);
		camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, fov, RealTime.deltaTime * rotationSpeed);
	}
}
